﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using PersonaLibrary.Controls;

namespace PersonaLibrary.View
{
	public static class FilterBindingAttached
	{
		public static readonly DependencyProperty FilterBindingProperty = DependencyProperty.RegisterAttached("FilterBinding", typeof(Predicate<object>),
			typeof(FilterBindingAttached), new PropertyMetadata(FilterChanged));

		public static void SetFilterBinding(UIElement element, Predicate<object> value)
		{
			ListBoxWithContentItem listBox = element as ListBoxWithContentItem;

			if(listBox != null)
				listBox.Collection.Filter = value;
		}

		public static Predicate<object> GetFilterBinding(UIElement element)
		{
			ListBoxWithContentItem listBox = element as ListBoxWithContentItem;

			if (listBox != null)
				return listBox.Collection.Filter;

			return null;
		}

		private static void FilterChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
		{
			ListBoxWithContentItem listBox = dependencyObject as ListBoxWithContentItem;

			if (listBox != null)
				listBox.Collection.Filter = (Predicate<object>)e.NewValue;
		}
	}
}
